package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private static TeacherService teacherService = new TeacherService();
    private static Scanner sc = new Scanner(System.in);
    public static void start(){
        //Scanner sc = new Scanner(System.in);
        studentLoop:
        while (true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    //System.out.println("添加");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用教师管理系统！");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入。。。");
                    break;
            }
        }
    }

    private static void findAllTeacher() {
        Teacher[] stus =teacherService.findAllTeacher();
        if (stus == null){
            System.out.println("查无信息，请重新输入:");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i <stus.length ; i++) {
            Teacher stu =stus[i];
            if (stu != null){
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }

        }
    }

    public static void updateTeacher() {
        String updateId;
        while (true){
            System.out.println("请输入您要修改的教师ID");
            updateId=sc.next();
            boolean exists = teacherService.isExists(updateId);
            if (!exists){
                System.out.println("您输入的ID不存在，请重新输入:");
            }else {
                break;
            }
        }
        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师年龄:");
        String age = sc.next();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();

        Teacher newStu = new Teacher();
        newStu.setId(updateId);
        newStu.setName(name);
        newStu.setAge(age);
        newStu.setBirthday(birthday);
        TeacherService.updateStudent(updateId,newStu);

    }

    public static void deleteTeacherById() {
        String delId;
        while (true){
            System.out.println("请输入您要删除的教师ID:");
            delId = sc.next();
            boolean exists = teacherService.isExists(delId);
            if (!exists){
                System.out.println("您输入的ID不存在，请重新输入:");
            }else {
                break;
            }
        }
        teacherService.deleteTeacherById(delId);
        System.out.println("删除成功！！");
    }

    public static void addTeacher() {
        String id;
        while (true){
            System.out.println("请输入教师ID");
            id = sc.next();
            boolean flag = TeacherService.isExists(id);
            if (flag){
                System.out.println("您输入的教师ID已被占用，请重新输入:");
            }else {
                break;
            }
        }
        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师年龄:");
        String age = sc.next();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();
        Teacher stu = new Teacher();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        boolean resuit = TeacherService.addTeacher(stu);
        if (resuit){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
}
